package com.yl.rss;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;

import com.yl.ifanr.db.RssItemBean;
import com.yl.rss.RssFeed;
import com.yl.util.TimeUitl;

public class RssHandler extends DefaultHandler {
		private static final String TAG = "RssHandler";
		private RssFeed rssFeed;
		private RssItemBean rssItem;
		
		private final int RSS_TITLE = 1;
		private final int RSS_LINK = 2;
		private final int RSS_DESCRIPTION = 3;
		private final int RSS_CONTENT = 4;
		private final int RSS_CATEGORY = 5;
		private final int RSS_PUBDATE = 6;
		private final int RSS_LASTBUILDDATE = 7;
		
		int currentstate = 0;

		public RssHandler(){
		}
		/**
		 * 返回源实例
		 * @return
		 */
		public RssFeed getFeed()
		{
			return rssFeed;
		}
		
		
		public void startDocument() throws SAXException
		{
			rssFeed = new RssFeed();
			rssItem = new RssItemBean();

		}
		public void endDocument() throws SAXException
		{
		}
		/**
		 * 开始解析节点
		 */
		public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
		{
			if (localName.equals("channel"))
			{
				currentstate = 0;
				return;
			}
			if (localName.equals("item"))
			{
				rssItem = new RssItemBean();
				return;
			}
			if (localName.equals("title"))
			{
				currentstate = RSS_TITLE;
				return;
			}
			if (localName.equals("description"))
			{
				currentstate = RSS_DESCRIPTION;
				return;
			}
			if (localName.equals("encoded"))//content:encoded
			{
				currentstate = RSS_CONTENT;
				return;
			}
			if (localName.equals("link"))
			{
				currentstate = RSS_LINK;
				return;
			}
			if (localName.equals("category"))
			{
				currentstate = RSS_CATEGORY;
				return;
			}
			if (localName.equals("pubDate"))
			{
				currentstate = RSS_PUBDATE;
				return;
			}
			if (localName.equals("lastBuildDate"))
			{
				currentstate = RSS_LASTBUILDDATE;
				return;
			}
			
			currentstate = 0;
		}
		
		public void endElement(String namespaceURI, String localName, String qName) throws SAXException
		{
			
			//如果解析一个item节点结束，就将RssItem添加到RSSFeed中。
			if (localName.equals("item")){
				rssFeed.addItem(rssItem);
				return;
			}
		}
		 
		public void characters(char ch[], int start, int length)
		{
			String theString = new String(ch,start,length);
			
			switch (currentstate)
			{
				case RSS_TITLE:
					rssItem.setTitle(theString);
					currentstate = 0;
					break;
				case RSS_LINK:
					rssItem.setLink(theString);
					currentstate = 0;
					break;
				case RSS_DESCRIPTION:
					theString = this.stripHtml(theString);
					rssItem.setDescription(theString);
					currentstate = 0;
					break;
				case RSS_CONTENT:
					rssItem.setContent(theString);
					currentstate = 0;
					break;
				case RSS_PUBDATE:
					rssItem.setPubdate(TimeUitl.getTime(theString));
					currentstate = 0;
					break;
				case RSS_LASTBUILDDATE:
					rssItem.setLastBuildDate(TimeUitl.getTime(theString));
					currentstate = 0;
					break;
				default:
					return;
			}
			
		}
		/**
		 * 去掉字符串里面的html代码。
		 */
		public String stripHtml(String content) {
			// <p>段落替换为换行
			content = content.replaceAll("<p .*?>", "\r\n");
			// <br><br/>替换为换行 
			content = content.replaceAll("<br\\s*/?>", "\r\n");
			// 去掉其它的<>之间的东西 
			content = content.replaceAll("\\<.*?>", "");
			// 还原HTML 
			// content = HTMLDecoder.decode(content);
			return content; 
		}
	}